package com.example.mydiary;

import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDiaryDatabase {
    private DatabaseHelper mOpenHelper;
    static final String Database_name = "eBook.db";//数据库名称
    static final int Database_Version = 1;
    static final String TABLE_NAME = "mydiary";    //数据表名称，日记
    static final String ID = "id";                 //ID编号，数据表的主键
    static final String BODY = "body";             //正文
    static final String TIME= "time";              //时间
    private SQLiteDatabase db;
    private Context context;
    //数据库的版本管理类
    private static class DatabaseHelper extends SQLiteOpenHelper {
        private DatabaseHelper(Context context) {
            super(context, Database_name, null, Database_Version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
    /*
     * 构造器，传递 Context, 例如 DiaryListActivity.java 程序中调用 MyDiaryDatabase（DiaryListActivity.this）
     */
    public MyDiaryDatabase(Context context) {
        mOpenHelper = new DatabaseHelper(context);
    }
    /*
     * 打开或创建数据库
     */
    public boolean opendb() {
        int mode = Context.MODE_PRIVATE;
        try {
            db = context.openOrCreateDatabase(Database_name, mode, null);//创建数据库
            Log.i("SQLite","打开或创建数据库");
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    /*
     * 创建数据表
     */
    public boolean onCreateTable() {
        //数据表如不存在就创建
        String DATABASE_CREATE =
                "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
                        + ID + " INTEGER primary key autoincrement,"//ID自动编号
                        + BODY + " text not null,"
                        + TIME + " text not null);";
                        //+ TIME2 + " text not null );";
        try {
            db.execSQL(DATABASE_CREATE);//创建数据表
            Log.i("SQLite","创建数据表");
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    /*
     * 删除数据库
     */
    public void deleteDB() {
        context.deleteDatabase(Database_name);
        Log.i("SQLite","删除数据库");
    }

    /*
     * 插入数据
     */
    public boolean insertdata(ContentValues values) {
        try {
            db.insert(TABLE_NAME,ID,values);
            Log.i("SQLite","插入数据记录");
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    /*
     * 删除符合条件的数据
     */
    public boolean deletedata(Integer id) {
        try {
            String args[] = {id.toString()};
            db.delete(TABLE_NAME, ID +"= ?", args);//  ? 代表一个参数，先占位
            Log.i("SQLite","删除符合条件的数据");
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    /*
     * 修改数据
     */
    public boolean update(ContentValues values,Integer id) {
        try {
            String args[] = {id.toString()};
            db.update(TABLE_NAME,values,ID + "= ?", args);
            Log.i("SQLite","修改数据");
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    /*
     * 获取符合ID 条件的记录，精确查询
     */
    public Cursor getdata(Integer id) {
        String args[] = {id.toString()};
        String col[] = {  BODY,TIME };
        //String col[] = {  BODY,TIME,TIME2 };
        Cursor cur = db.query(TABLE_NAME, col, ID + "= ?", args, null, null, null);
        Log.i("SQLite","获取符合条件的数据");
        return cur;
    }
    /*
     *   获取body 符合条件的记录，模糊查询
     */
    public Cursor sousuo(String s) {
       String sql="select * from "+TABLE_NAME+" where body like '%"+s+"%'";
        Cursor cur = db.rawQuery(sql,null);
        Log.i("SQLite","获取符合条件的数据");
        return cur;
    }
    /*
     * 获取数据表的全部记录
     */
    public Cursor getAllNotes() {
        String col[] = { ID, BODY,TIME };
        // String col[] = { ID, BODY,TIME,TIME2 };
        Log.i("SQLite","获取全部记录");
        return db.query(TABLE_NAME, col, null, null, null, null, null);
    }
}